package com.yy.controller;

import com.yy.domain.MemberAddr;
import com.yy.model.Result;
import com.yy.service.MemberAddrService;
import com.yy.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述: [会员的地址管理接口]
 * 作者: yy
 */
@RestController
@Api(tags = "会员的地址管理接口")
@RequestMapping("p/address")
public class MemberAddrController {
    @Autowired
    public MemberAddrService memberAddrService;

    @GetMapping("list")
    @ApiOperation("会员的收获地址")
    public Result<List<MemberAddr>> getMemberAddrs() {
        String openId = AuthUtil.getOpenId();
        List<MemberAddr> memberAddrs = memberAddrService.getMemberAddrs(openId);
        return Result.success(memberAddrs);
    }


    @PostMapping
    @ApiOperation("新增收获地址")
    public Result<String> addMemberAddrs(@RequestBody @Validated MemberAddr memberAddr) {
        String openId = AuthUtil.getOpenId();
        memberAddr.setOpenId(openId);
        Integer i = memberAddrService.addMemberAddrs(memberAddr);
        return Result.handle(i > 0);
    }

    @PutMapping("defaultAddr/{id}")
    @ApiOperation("修改会员的默认收货地址")
    public Result<String> changeDefaultMemberAddr(@PathVariable("id") Long id) {
        int i = memberAddrService.changeDefaultMemberAddr(id);
        return Result.handle(i > 0);
    }
}
